import request from './request'
import type { AxiosResponse } from 'axios'

/**
 * 用户信息接口
 */
interface UserInfo {
  userId: number
  username: string
  backgroundImage?: string
  backgroundColor?: string
}

/**
 * 认证响应接口
 */
interface AuthResponse {
  success: boolean
  message: string
  data?: {
    token: string
    user: UserInfo
  }
}

/**
 * 用户信息响应接口
 */
interface UserResponse {
  success: boolean
  message?: string
  user?: UserInfo
}

/**
 * 背景图片上传响应接口
 */
interface BackgroundUploadResponse {
  success: boolean
  message: string
  backgroundUrl?: string
}

/**
 * 背景色更新响应接口
 */
interface BackgroundColorResponse {
  success: boolean
  message: string
  backgroundColor?: string
}

/**
 * 用户相关API
 */
export const userApi = {
  /**
   * 用户注册
   */
  register: async (username: string, password: string): Promise<AuthResponse> => {
    try {
      const response = await request.post('/user/register', {
        username,
        password
      })
      return response.data
    } catch (error: any) {
      return {
        success: false,
        message: error.response?.data?.message || '注册失败',
        data: undefined
      }
    }
  },

  /**
   * 用户登录
   */
  login: async (username: string, password: string): Promise<AuthResponse> => {
    try {
      const response = await request.post('/user/login', {
        username,
        password
      })
      return response.data
    } catch (error: any) {
      return {
        success: false,
        message: error.response?.data?.message || '登录失败',
        data: undefined
      }
    }
  },

  /**
   * 获取用户信息
   */
  getUserInfo: async (): Promise<UserResponse> => {
    try {
      const response = await request.get('/user/info')
      if (response.data.success && response.data.data) {
        return {
          success: true,
          user: {
            userId: response.data.data.userId,
            username: response.data.data.username,
            backgroundImage: response.data.data.backgroundImage,
            backgroundColor: response.data.data.backgroundColor
          }
        }
      }
      return response.data
    } catch (error: any) {
      return {
        success: false,
        message: error.response?.data?.message || '获取用户信息失败'
      }
    }
  },

  /**
   * 上传背景图片
   * @param file 图片文件
   */
  uploadBackground: async (file: File): Promise<BackgroundUploadResponse> => {
    try {
      const formData = new FormData()
      formData.append('file', file)

      const response = await request.post('/user/background', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      
      // 适配新的返回格式
      if (response.data.success && response.data.data) {
        return {
          success: true,
          message: response.data.message,
          backgroundUrl: response.data.data.backgroundUrl
        }
      }
      
      return response.data
    } catch (error: any) {
      return {
        success: false,
        message: error.response?.data?.message || '上传背景图片失败'
      }
    }
  },

  /**
   * 更新背景色
   * @param backgroundColor 十六进制颜色代码（如 #FFFFFF）
   */
  updateBackgroundColor: async (backgroundColor: string): Promise<BackgroundColorResponse> => {
    try {
      const response = await request.post('/user/background-color', {
        backgroundColor
      })
      return response.data
    } catch (error: any) {
      return {
        success: false,
        message: error.response?.data?.message || '更新背景色失败'
      }
    }
  }
}

// 更新类型导出
export type { 
  UserInfo, 
  AuthResponse, 
  UserResponse, 
  BackgroundUploadResponse,
  BackgroundColorResponse  // 添加新的类型导出
}
